#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Library.Entity;
using Library.Services;
using System.Collections;

#endregion

namespace Library
{
    public partial class UserControlBookStockAdjustment : UserControl
    {
        BookTransactionService m_BookTransactionService = new BookTransactionService();
        User m_currentUser;
        public UserControlBookStockAdjustment(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            this.Name = name;
            this.Text = name;
            labelTitle.Text = name;
            m_currentUser = currentUser; 
            initColumn();
            initData();
            initButtonClick();
            toolBarButtonDelete.Enabled = listView.SelectedItem != null;
        }

        private void initButtonClick()
        {
            toolBarButtonAddNew.Click += new EventHandler(toolBarButtonAddNew_Click);
            toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                MessageBox.Show("Are you sure to delete?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, new EventHandler(DeleteData));
            }
        }

        void DeleteData(object sender, EventArgs e) 
        {
            try
            {
                MessageBoxWindow msg = (MessageBoxWindow)sender;
                if (msg.DialogResult == DialogResult.Yes)
                {
                    BookTransaction BookStock = (BookTransaction)listView.SelectedItem.Tag;
                    if (BookStock.Id > 0)
                    {
                        m_BookTransactionService.DeleteBookTransaction(BookStock);
                        MessageBox.Show("Record has been deleted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                        listView.Items.Remove(listView.SelectedItem);
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void toolBarButtonAddNew_Click(object sender, EventArgs e)
        {
            FormBookStockAdjustment frm = new FormBookStockAdjustment(new BookTransaction(), m_currentUser, this);
            frm.ShowDialog();
        }
       
        private void initColumn()
        {
            listView.Columns.Add("Code", 75, HorizontalAlignment.Left);
            listView.Columns.Add("Transaction Date", 75, HorizontalAlignment.Left);
            listView.Columns.Add("Title", 100, HorizontalAlignment.Left);
            listView.Columns.Add("Transaction Type", 100, HorizontalAlignment.Left);
            listView.Columns.Add("Qty", 50, HorizontalAlignment.Right);
            listView.Columns.Add("Remarks", 200, HorizontalAlignment.Left);
        }

        private void initData()
        {
            listView.Items.Clear();
            ArrayList data = new ArrayList();
            if (checkBoxCode.Checked)
            {
                if (checkBoxDAte.Checked)
                {
                    data = (ArrayList)m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Purchase, dateTimePickerTrDate.Value, textBoxBookCode.Text);
                    data.AddRange(m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Donation, dateTimePickerTrDate.Value, textBoxBookCode.Text));
                    data.AddRange(m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Lost, dateTimePickerTrDate.Value, textBoxBookCode.Text));
                    data.AddRange(m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Broken, dateTimePickerTrDate.Value, textBoxBookCode.Text));
                }
                else
                {
                    data = (ArrayList)m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Purchase, textBoxBookCode.Text);
                    data.AddRange(m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Donation, textBoxBookCode.Text));
                    data.AddRange(m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Lost, textBoxBookCode.Text));
                    data.AddRange(m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Broken, textBoxBookCode.Text));
                }
            }
            else if (checkBoxDAte.Checked)
            {
                data = (ArrayList)m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Purchase, dateTimePickerTrDate.Value);
                data.AddRange(m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Donation, dateTimePickerTrDate.Value));
                data.AddRange(m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Lost, dateTimePickerTrDate.Value));
                data.AddRange(m_BookTransactionService.GetAllBookTransactionByTransactionType(TransactionType.Broken, dateTimePickerTrDate.Value));
            }
            foreach(BookTransaction BookStock in data)
            {
                AddNewData(BookStock);
            }
        }

        internal void AddNewData(BookTransaction BookStock)
        {
            ListViewItem item = listView.Items.Add(BookStock.Code);
            item.SubItems.Add(BookStock.TransactionDate.ToString("dd-MMM-yyyy"));
            item.SubItems.Add(BookStock.Name);
            item.SubItems.Add(BookStock.TransactionType.ToString());
            item.SubItems.Add(BookStock.Quantity.ToString());
            item.SubItems.Add(BookStock.Remark);
            item.Tag = BookStock;
        }

        private void listView_DoubleClick(object sender, EventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                BookTransaction BookStock = (BookTransaction)listView.SelectedItem.Tag;
                FormBookStockAdjustment frm = new FormBookStockAdjustment(BookStock, m_currentUser, this);
                frm.ShowDialog();
            }
        }

        internal void UpdateData(BookTransaction BookStock)
        {
            listView.SelectedItem.SubItems[0].Text = BookStock.Code;
            listView.SelectedItem.SubItems[1].Text = BookStock.TransactionDate.ToString("dd-MMM-yyyy");
            listView.SelectedItem.SubItems[2].Text = BookStock.Name;
            listView.SelectedItem.SubItems[3].Text = BookStock.TransactionType.ToString();
            listView.SelectedItem.SubItems[4].Text = BookStock.Quantity.ToString();
            listView.SelectedItem.SubItems[4].Text = BookStock.Remark;
            listView.SelectedItem.Tag = BookStock;
        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolBarButtonDelete.Enabled = listView.SelectedItem != null;
        }

        private void buttonFInd_Click(object sender, EventArgs e)
        {
            initData();
        }

        private void textBoxBookCode_EnterKeyDown(object objSender, KeyEventArgs objArgs)
        {
            if(objArgs.KeyData == Keys.Enter)
                initData();
        }
    }
}